/*
 * 将进度条转换成为时间轴
 */
package com.sysu.minsysu.zdkb.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.util.Log;

public class SeekbarTime {
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");       
	private Date curDate,queryDate; 
	private String now, pass, select; 
	
	
	public SeekbarTime(){
		setCurrentTime();
		setOneHourAgoTime();
	}
	
	public String changeSeekbarToTime(int percent){		
		long Time = (curDate.getTime()/1000) - (100 - percent)*60;
//		Log.v("current date", String.valueOf((curDate.getTime())));
//		Log.v("current date/1000", String.valueOf((curDate.getTime()/1000)));
//		Log.v("select date/1000", String.valueOf(Time));
//		Log.v("select date", String.valueOf(Time*1000));
		queryDate = new Date();
		queryDate.setTime(Time*1000);
		select = formatter.format(queryDate);
		
		Log.v("chage to time", "now:" + now + " select:" + select + " pecent:" + String.valueOf(percent));
		return select;
	}
	
//	public String getSelectTime(){
//		return select;
//	}
	
	/*
	 * 获取一段时间
	 */
	public String[] getRangeTime(String time) {
		String[] range = new String[2];
		
		Date date = new Date();
		try {
			date = formatter.parse(time);			
			long nowLong = date.getTime();//将参考日期转换为毫秒时间  
			
			
			//暂时选择一个较大的数字
			Date time1 = new Date(nowLong - formatToTimeMillis(100, 2));			
		    Date time2 = new Date(nowLong + formatToTimeMillis(100, 2));//加上时间差毫秒数  
		    
		    range[0] = formatter.format(time1);
		    Log.e("range[0]", range[0]);
		    range[1] = formatter.format(time2);
		    Log.e("range[1]", range[1]);
		    
		    return range;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			    
	    
		return null;
	}
	
	public void setCurrentTime(){
		curDate = new Date(System.currentTimeMillis());//获取当前时间     
		now = formatter.format(curDate);
	}
	
	public void setOneHourAgoTime() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR, 1);
		
		pass = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss").format(cal.getTime());

	}
	
	/** 
	 * 将一个表示时间段的数转换为毫秒数 
	 * @param num 时间差数值,支持小数 
	 * @param type 时间类型：1->秒,2->分钟,3->小时,4->天 
	 * @return long类型时间差毫秒数，当为-1时表示参数有错 
	 */  
	public long formatToTimeMillis(double num, int type) {  
	    if (num <= 0)  
	        return 0;  
	    switch (type) {  
		    case 1:  
		        return (long) (num * 1000);  
		    case 2:  
		        return (long) (num * 60 * 1000);  
		    case 3:  
		        return (long) (num * 60 * 60 * 1000);  
		    case 4:  
		        return (long) (num * 24 * 60 * 60  * 1000);  
		    default:  
		        return -1;  
	    }  
	} 
}
